home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* */
- /* Application: QuickDraw™ FX */
- /* */
- /* Description: */
- /* */
- /* File: FX.c */
- /* */
- /* Files: FX.π */
- /* FX.π.rsrc */
- /* FX.h */
- /* events.c */
- /* main.c */
- /* menu.c */
- /* */
- /* Programmer: Edgar Lee */
- /* Organization: Apple Computer, Inc. ©1992 */
- /* Department: Developer Technical Support, DTS */
- /* Language: C (Think C version 5.0.2) */
- /* Date Created: 5-26-92 */
- /* */
- /****************************************************************************/
-
- #include "FX.h"
-
- void transferExample();
- void arithmeticExample();
- void colorizationExample();
- void ditherExample();
- void mappingExample();
- void paintBucketExample();
- void lassoToolExample();
- void pixelAverageExample();
- void customExample();
-
- GrafPtr CreateGrafPort();
- void DisposeGrafPort();
-
- pascal Boolean searchProc();
-
- long drawFXImage()
- {
- int exampleNumber, exampleItem;
- Rect rect, rect2;
- Rect outlineRect;
- long startTicks, endTicks;
- Point point = { 0, 0 };
- Rect frame;
-
- SetRect( &rect, (*gWindow).portRect.right - (*gGWorld).portRect.right - 20, 37,
- (*gWindow).portRect.right - 20, 37 + (*gGWorld).portRect.bottom );
-
- outlineRect = rect;
- InsetRect( &outlineRect, -5, -5 );
- drawDeepBox( &outlineRect );
-
- frame = rect;
-
- if (settings.bItem == 1)
- {
- ForeColor( whiteColor );
- PaintRect( &rect );
- }
- else if (settings.bItem == 2)
- {
- ForeColor( blackColor );
- PaintRect( &rect );
- }
- else
- {
- rect2 = rect;
-
- rect2.right = (rect2.right - rect2.left) / 2 + rect2.left;
- ForeColor( whiteColor );
- PaintRect( &rect2 );
-
- rect2.left = rect2.right;
- rect2.right = rect.right;
- ForeColor( blackColor );
- PaintRect( &rect2 );
- }
-
- ForeColor( blackColor );
- BackColor( whiteColor );
-
- exampleNumber = gCurrentExample / 10;
- exampleItem = gCurrentExample % 10;
-
- startTicks = TickCount();
-
- if (exampleNumber == 1)
- transferExample( &rect, exampleItem );
- else if (exampleNumber == 2)
- arithmeticExample( &rect, exampleItem );
- else if (exampleNumber == 3)
- ditherExample( &rect, exampleItem );
- else if (exampleNumber == 4)
- colorizationExample( &rect, exampleItem );
- else if (exampleNumber == 5)
- mappingExample( &rect, exampleItem );
- else if (exampleNumber == 6)
- paintBucketExample( &rect, exampleItem, point );
- else if (exampleNumber == 7)
- lassoToolExample( &rect, exampleItem, &frame );
- else if (exampleNumber == 8)
- pixelAverageExample( &rect, exampleItem );
- else if (exampleNumber == 9)
- customExample( &rect, exampleItem );
-
- endTicks = TickCount();
-
- return (endTicks - startTicks);
- }
-
- void transferExample( rect, item )
- Rect *rect;
- int item;
- {
- int transferMode;
-
- resetItems();
- settings.tItem = item;
-
- transferMode = setTransferMode( item );
-
- CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
- &(**(*gGWorld).portPixMap).bounds, rect, transferMode, nil );
- }
-
- int setTransferMode( item )
- int item;
- {
- item--;
- return item;
- }
-
- void arithmeticExample( rect, item )
- Rect *rect;
- int item;
- {
- int arithmeticMode;
- RGBColor color;
-
- resetItems();
- settings.aItem = item;
-
- arithmeticMode = setArithmeticMode( item );
-
- CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
- &(**(*gGWorld).portPixMap).bounds, rect, arithmeticMode, nil );
-
- color.red = color.green = color.blue = 0;
- OpColor( &color );
- }
-
- int setArithmeticMode( item )
- int item;
- {
- RGBColor color;
-
- if (item <= 4)
- item = 31 + item;
- else if (item == 9)
- item = transparent;
- else
- item = 32 + item;
-
- color.red = color.green = color.blue = 0xffff / 2;
- OpColor( &color );
-
- return item;
- }
-
- void colorizationExample( rect, item )
- Rect *rect;
- int item;
- {
- int i;
- Rect srcRect, dstRect;
- RGBColor color;
-
- resetItems();
- settings.cItem = item;
-
- srcRect = (**(*gGWorld).portPixMap).bounds;
- srcRect.right = srcRect.left + (srcRect.right - srcRect.left) / 3;
-
- dstRect = *rect;
- dstRect.right = dstRect.left + (dstRect.right - dstRect.left) / 3;
-
- for (i = 0; i < 3; i++)
- {
- if (item == 1)
- {
- ForeColor( blackColor );
- BackColor( whiteColor );
- }
- else if (item == 2)
- {
- ForeColor( whiteColor );
- BackColor( blackColor );
- }
- else
- {
- color.red = color.green = color.blue = 0;
-
- if (i == 0)
- color.red = 0xffff;
- else if (i == 1)
- color.green = 0xffff;
- else if (i == 2)
- color.blue = 0xffff;
-
- if (item == 3)
- {
- RGBForeColor( &color );
- BackColor( whiteColor );
- }
- else
- {
- RGBBackColor( &color );
- ForeColor( blackColor );
- }
- }
-
- CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
- &srcRect, &dstRect, srcCopy, nil );
-
- OffsetRect( &srcRect, srcRect.right - srcRect.left, 0 );
- OffsetRect( &dstRect, dstRect.right - dstRect.left, 0 );
- }
- }
-
- void ditherExample( rect, item )
- Rect *rect;
- int item;
- {
- int ditherMode;
-
- /* This function shows an example of using copybits with the dithering. */
-
- resetItems();
- settings.dItem = item;
-
- ditherMode = setDitherMode( item );
-
- CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
- &(**(*gGWorld).portPixMap).bounds, rect, srcCopy + ditherMode, nil );
- }
-
- int setDitherMode( item )
- int item;
- {
- if (item == 1)
- return 0;
- else
- return ditherCopy;
- }
-
- void mappingExample( rect, item )
- Rect *rect;
- int item;
- {
- int i;
- CTabHandle ctable;
- GWorldPtr gworld;
- CTabHandle ictable;
-
- resetItems();
- settings.mItem = item;
-
- if (item == 1)
- CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
- &(**(*gGWorld).portPixMap).bounds, rect, srcCopy, nil );
- else if (item == 2)
- {
- AddSearch( searchProc );
-
- CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
- &(**(*gGWorld).portPixMap).bounds, rect, srcCopy, nil );
-
- DelSearch( searchProc );
- }
- else if (item == 3)
- {
- NewGWorld( &gworld, 8, &(*gGWorld).portRect, GetCTable( 8 + 64 ), nil, 0 );
-
- CopyBits( (BitMap *)(*(*gGWorld).portPixMap), (BitMap *)(*(*gworld).portPixMap),
- &(**(*gGWorld).portPixMap).bounds, &(**(*gworld).portPixMap).bounds,
- srcCopy, nil );
-
- ictable = GetCTable( 8 );
-
- for (i = 0; i <= (**(**(*gworld).portPixMap).pmTable).ctSize; i++)
- (**(**(*gworld).portPixMap).pmTable).ctTable[i].rgb =
- (**ictable).ctTable[255 - i].rgb;
-
- (**(**(*gworld).portPixMap).pmTable).ctSeed = 67;
- // (**(**(*(CGrafPtr)gWindow).portPixMap).pmTable).ctSeed;
-
- CopyBits( (BitMap *)(*(*gworld).portPixMap), &gWindow->portBits,
- &(**(*gworld).portPixMap).bounds, rect, srcCopy, nil );
-
- DisposeGWorld( gworld );
- }
- }
-
- pascal Boolean searchProc( color, position )
- RGBColor *color;
- long *position;
- {
- (*color).red = 0;
-
- return false;
- }
-
- void paintBucketExample( rect, exampleItem, point )
- Rect *rect;
- int exampleItem;
- Point point;
- {
- GrafPtr mask;
- CGrafPtr currentPort;
- GDHandle currentDevice;
- Point newPoint;
- GWorldPtr gworld;
-
- resetItems();
-
- newPoint.h = point.h - 20;
- newPoint.v = point.v - 37;
-
- if (newPoint.h < 0 || newPoint.v < 0 ||
- newPoint.h > ((**(*gGWorld).portPixMap).bounds.right - (**(*gGWorld).portPixMap).bounds.left) ||
- newPoint.v > ((**(*gGWorld).portPixMap).bounds.bottom - (**(*gGWorld).portPixMap).bounds.top))
- return;
-
- mask = CreateGrafPort( &(*gGWorld).portRect );
-
- ForeColor( redColor );
- MoveTo( point.h - 2, point.v );
- LineTo( point.h + 2, point.v );
- MoveTo( point.h, point.v - 2 );
- LineTo( point.h, point.v + 2 );
-
- ForeColor( blackColor );
-
- NewGWorld( &gworld, 8, &(*gGWorld).portRect, GetCTable( 8 + 64 ), nil, 0 );
-
- CopyBits( (BitMap *)(*(*gGWorld).portPixMap), (BitMap *)(*(*gworld).portPixMap),
- &(**(*gGWorld).portPixMap).bounds, &(**(*gworld).portPixMap).bounds,
- srcCopy, nil );
-
- // GetGWorld( ¤tPort, ¤tDevice );
- // SetGWorld( gworld, nil );
-
- SeedCFill( (BitMap *)(*(*gworld).portPixMap), &mask->portBits,
- &(**(*gworld).portPixMap).bounds, &mask->portRect,
- newPoint.h, newPoint.v, nil, 0 );
- // SetGWorld( currentPort, currentDevice );
-
- CopyMask( (BitMap *)(*(*gworld).portPixMap), &mask->portBits, &gWindow->portBits,
- &(**(*gworld).portPixMap).bounds, &mask->portRect, rect );
-
- DisposeGrafPort( mask );
- }
-
- void lassoToolExample( rect, exampleItem, frame )
- Rect *rect;
- int exampleItem;
- Rect *frame;
- {
- Rect bounds;
- GrafPtr mask;
- RGBColor color;
- CGrafPtr currentPort;
- GDHandle currentDevice;
-
- pascal Boolean matchProc();
-
- resetItems();
-
- mask = CreateGrafPort( &(*gGWorld).portRect );
-
- GetGWorld( ¤tPort, ¤tDevice );
- SetGWorld( gGWorld, nil );
-
- color.red = color.green = color.blue = 0;
-
- CalcCMask( (BitMap *)(*(*gGWorld).portPixMap), &mask->portBits,
- &(**(*gGWorld).portPixMap).bounds, &mask->portRect,
- &color, matchProc, 0 );
- SetGWorld( currentPort, currentDevice );
-
- CopyMask( (BitMap *)(*(*gGWorld).portPixMap), &mask->portBits, &gWindow->portBits,
- &(**(*gGWorld).portPixMap).bounds, &mask->portRect, rect );
- /*
- bounds = *frame;
- OffsetRect( &bounds, -bounds.left, -bounds.top );
-
- mask = CreateGrafPort( &bounds );
-
- GetGWorld( ¤tPort, ¤tDevice );
- SetGWorld( gGWorld, nil );
-
- color.red = color.green = color.blue = 0;
-
- CalcCMask( (BitMap *)(*(*gGWorld).portPixMap), &mask->portBits,
- &bounds, &mask->portRect, &color, matchProc, 0 );
-
- SetGWorld( currentPort, currentDevice );
-
- CopyMask( (BitMap *)(*(*gGWorld).portPixMap), &mask->portBits, &gWindow->portBits,
- &bounds, &mask->portRect, frame );
- */
-
- DisposeGrafPort( mask );
- }
-
- pascal Boolean matchProc( color, position )
- RGBColor *color;
- long *position;
- {
- GDHandle currDevice;
- MatchRec *matchInfo;
-
- currDevice = GetGDevice();
- matchInfo = (MatchRec *)(**currDevice).gdRefCon;
-
- if (matchInfo->red == color->red && matchInfo->green == color->green &&
- matchInfo->blue == color->blue)
- *position = 0;
- else
- *position = 1;
-
- return true;
- }
-
- void pixelAverageExample( rect, item )
- Rect *rect;
- int item;
- {
- Rect bounds;
- GWorldPtr gworld;
- GWorldPtr source;
-
- resetItems();
-
- /* Create 8-bit gworld from 32-bit original. */
-
- NewGWorld( &source, 8, &(*gGWorld).portRect, GetCTable( 8 + 64 ), nil, 0 );
- CopyBits( (BitMap *)(*(*gGWorld).portPixMap), (BitMap *)(*(*source).portPixMap),
- &(**(*gGWorld).portPixMap).bounds, &(**(*source).portPixMap).bounds,
- srcCopy, nil );
-
- /* Create another 8-bit gworld at quarter size of original. */
-
- SetRect( &bounds, 0, 0, (*gGWorld).portRect.right / 4,
- (*gGWorld).portRect.bottom / 4 );
-
- NewGWorld( &gworld, 8, &bounds, GetCTable( 8 + 64 ), nil, 0 );
-
- /* Copy the 8-bit original to 8-bit quarter image without pixel averaging. */
-
- bounds = (*source).portRect;
-
- CopyBits( (BitMap *)(*(*source).portPixMap), (BitMap *)(*(*gworld).portPixMap),
- &(**(*source).portPixMap).bounds, &(**(*gworld).portPixMap).bounds,
- srcCopy, nil );
-
- /* Copy the quarter image to screen. */
-
- OffsetRect( &bounds, 20, 37 );
- CopyBits( (BitMap *)(*(*gworld).portPixMap), &(*gWindow).portBits,
- &(**(*gworld).portPixMap).bounds, &bounds, srcCopy, nil );
-
- /* Now, copy the 8-bit original to 8-bit quarter image with pixel averaging. */
-
- bounds = (*source).portRect;
-
- CopyBits( (BitMap *)(*(*source).portPixMap), (BitMap *)(*(*gworld).portPixMap),
- &(**(*source).portPixMap).bounds, &(**(*gworld).portPixMap).bounds,
- srcCopy + ditherCopy, nil );
-
- /* Finally, copy pixel averaged quarter image to screen. */
-
- OffsetRect( &bounds, (*rect).left, (*rect).top );
- CopyBits( (BitMap *)(*(*gworld).portPixMap), &(*gWindow).portBits,
- &(**(*gworld).portPixMap).bounds, &bounds, srcCopy, nil );
-
- DisposeGWorld( source );
- DisposeGWorld( gworld );
- }
-
- void customExample( rect, item )
- Rect *rect;
- int item;
- {
- int transferMode;
- int ditherMode;
- RGBColor color;
-
- transferMode = setTransferMode( settings.tItem );
- ditherMode = setDitherMode( settings.dItem );
-
- if (settings.cItem == 1)
- {
- ForeColor( blackColor );
- BackColor( whiteColor );
- }
- else if (settings.cItem == 2)
- {
- ForeColor( whiteColor );
- BackColor( blackColor );
- }
- else if (settings.cItem == 3)
- {
- color.green = 0xffff;
- color.red = color.blue = 0;
- RGBForeColor( &color );
- BackColor( whiteColor );
- }
- else if (settings.cItem == 4)
- {
- color.green = 0xffff;
- color.red = color.blue = 0;
- ForeColor( blackColor );
- RGBBackColor( &color );
- }
-
- if (settings.mItem == 2)
- AddSearch( searchProc );
-
- CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
- &(**(*gGWorld).portPixMap).bounds, rect,
- transferMode + ditherMode, nil );
-
- if (settings.mItem == 2)
- DelSearch( searchProc );
- }
-
- GrafPtr CreateGrafPort( bounds ) /* Originally written by Forrest Tanaka. */
- Rect *bounds;
- {
- GrafPtr savedPort; /* Saved GrafPtr for later restore. */
- GrafPtr newPort; /* New GrafPort. */
- Rect localBounds; /* Local copy of bounds. */
-
- GetPort( &savedPort );
-
- /* Set the top-left corner of bounds to (0,0). */
- localBounds = *bounds;
- OffsetRect( &localBounds, -bounds->left, -bounds->top );
-
- /* Allocate a new GrafPort. */
- newPort = (GrafPtr)NewPtrClear( sizeof( GrafPort ) );
-
- if (newPort != nil)
- {
- /* Initialize the new port and make the current port. */
- OpenPort( newPort );
-
- /* Initialize and allocate the bitmap. */
- newPort->portBits.bounds = localBounds;
- newPort->portBits.rowBytes = ((localBounds.right + 15) >> 4) << 1;
- newPort->portBits.baseAddr = NewPtrClear( newPort->portBits.rowBytes *
- (long)localBounds.bottom );
- if (newPort->portBits.baseAddr != nil)
- {
- /* Clean up the new port. */
- newPort->portRect = localBounds;
- ClipRect( &localBounds );
- RectRgn( newPort->visRgn, &localBounds );
- EraseRect( &localBounds );
- }
- else
- {
- /* Allocation failed; deallocate the port. */
- ClosePort( newPort );
- DisposPtr( (Ptr)newPort );
- newPort = nil;
- }
- }
-
- SetPort( savedPort );
- return newPort;
- }
-
- void DisposeGrafPort( doomedPort ) /* Originally written by Forrest Tanaka. */
- GrafPtr doomedPort;
- {
- ClosePort( doomedPort );
- DisposPtr( doomedPort->portBits.baseAddr );
- DisposPtr( (Ptr)doomedPort );
- }